<?php

//普通上传
function upimage($upfile,$path,$imgthumb=1){
$maxfilesize = 2000000;
$thumbwidth	=200;
$thumbheight=150;
$foldera="up/pic/";
$thumb = 'thumb/';
$folder=$foldera.$path; //上传文件路径
	if($imgthumb!=="1"){
		$imgthumb=$imgthumb;	//是否生成缩略图(1为生成,其他为不生成);
	}
	$uptypes=array(
		'image/jpg',
		'image/jpeg',
		'image/png',
		'image/pjpeg',
		'image/gif',
		'image/bmp',
		'image/x-png');
	if (!is_uploaded_file($_FILES["$upfile"]["tmp_name"])){
		echo "图片不存在!";
		exit;
	}
	$file = $_FILES["$upfile"];
	$filenames=$file["name"];
	$filesizes=$file["size"];
	if($maxfilesize < $file["size"]){
		echo "文件太大!".$maxfilesize;
		exit;
	}
	if(!in_array($file["type"], $uptypes)){
		echo "文件类型不符!".$file["type"];
		exit;
	}
	if(!file_exists($folder)){mkdirm($folder);}
	if(!file_exists($folder.$thumb)){mkdirm($folder.$thumb);}
	$filename=$file["tmp_name"];
	$image_size = getimagesize($filename);
	$pinfo=pathinfo($file["name"]);
	$realname=$pinfo['filename'];
	$ftype=$pinfo['extension'];
	$savepath = $folder.$realname.".".$ftype;
	if (file_exists($savepath) && $overwrite != true){
		echo "同名文件已经存在了".time();
		exit;
	}
	if(!move_uploaded_file ($filename, $savepath)){
		echo "移动文件出错";
		exit;
	}
	//生成缩略图
	if($imgthumb==1){
		$pinfo=pathinfo($savepath);
		$savethumbpath=$folder.$thumb.$pinfo["basename"];
		thumb($savepath,$savethumbpath,$thumbwidth,$thumbheight);
	}
	$upimage['name']=$filenames;
	$upimage['path']=$folder;
	$upimage['thumb']=$thumb;
	$upimage['size']=$filesizes;
	return $upimage;
}
function filekzm($a){
	$c=strrchr($a,'.');
	if($c){
		return $c;
	}else{
		return '';
	}
}
function mkdirm($path){ 
	if(!file_exists($path)){
		mkdirm(dirname($path));
		mkdir($path, 0777);
	}
}
function thumb($savepath,$savethumbpath,$thumbwidth,$thumbheight){
			list($width,$height) = getimagesize($savepath);
			if($width>$thumbwidth || $height>$thumbheight){
				$matchimg= $thumbwidth*$height/$thumbheight;
				if($width>$matchimg){
					$newheight=($thumbwidth*$height)/$width;
					$newwidth=$thumbwidth;
					$fheight=($thumbheight-$newheight)/2;
					$fwidth=0;
				}else if($width<$matchimg){
					if($thumbwidth<$thumbheight){
						$newwidth=($thumbheight*$width)/$thumbwidth;
						$newheight=$thumbheight;
						$fwidth=($thumbwidth-$newwidth)/2;
						$fheight=0;
					}else{
						$newwidth=$width*$thumbheight/$height;
						$newheight=$thumbheight;
						$fwidth=($thumbwidth-$newwidth)/2;
						$fheight=0;
					}
				}else if($width==$matchimg){
					$newwidth=$thumbwidth;
					$newheight=$thumbheight;
					$fwidth=0;
					$fheight=0;
				}
				$thumb = imagecreatetruecolor($newwidth, $newheight);
				$white=imagecolorallocate($thumb,255,255,255);
				$black=imagecolorallocate($thumb,0,0,0);
				$red=imagecolorallocate($thumb,255,0,0);
				imagefill($thumb,0,0,$white);
				
			$iinfo=getimagesize($savepath,$iinfo);
			switch ($iinfo[2]){
				case 1:
				$source =imagecreatefromgif($savepath);
				break;
				case 2:
				$source =imagecreatefromjpeg($savepath);
				break;
				case 3:
				$source =imagecreatefrompng($savepath);
				break;
				case 6:
				$source =imagecreatefromwbmp($savepath);
				break;
				default:
				die("不支持的文件类型");
				exit;
			}
				//$source = imagecreatefromjpeg($savepath);
				imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
				switch ($iinfo[2]){
					case 1:
					imagegif($thumb, $savethumbpath);
					//imagejpeg($thumb, $savethumbpath);
					echo 'gif';
					break;
					case 2:
					imagejpeg($thumb, $savethumbpath);
					break;
					case 3:
					imagepng($thumb, $savethumbpath);
					break;
					case 6:
					imagewbmp($thumb, $savethumbpath);
					//imagejpeg($thumb, $savethumbpath);
					break;
				}
				return $upimg=1;
			}else{
				return $upimg=0;
			}
}
//	if ($_SERVER['REQUEST_METHOD'] == 'POST'){
//		$upfile = 'upfile';
//		$upimage = upimage($upfile);
//	}

//随机函数
function randpw($length)
{
$output='';
for ($a = 0; $a < $length; $a++) {
$output .= chr(mt_rand(33, 126));    //生成php随机数
}
return $output;
}
//删除
function Del_file($path){
	if(file_exists($path)){
		unlink($path);
	}
}
?>